`:top
The `!Luhn algorithm`! or `!Luhn formula`! (creator: `F33f`_`[IBM`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=IBM]`_`f scientist `F33f`_`[Hans Peter Luhn`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Hans_Peter_Luhn]`_`f), also known as the "`F33f`_`[modulus`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Modular_arithmetic]`_`f 10" or "mod 10" `F33f`_`[algorithm`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Algorithm]`_`f, is a simple `F33f`_`[check digit`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Check_digit]`_`f formula used to validate a variety of identification numbers. `:cite-ref-2[`F5bf`_`[a`#cite-note-2]`_`f]
The algorithm is in the `F33f`_`[public domain`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Public_domain]`_`f and is in wide use today. It is specified in `F33f`_`[ISO/IEC 7812-1`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISO/IEC_7812-1]`_`f.`:cite-ref-3[`F5bf`_`[2`#cite-note-3]`_`f] It is not intended to be a `F33f`_`[cryptographically secure hash function`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Cryptographic_hash_function]`_`f; it was designed to protect against accidental errors, not malicious attacks. Most `F33f`_`[credit card numbers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Credit_card_number]`_`f and many `F33f`_`[government identification numbers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Government_identification_numbers]`_`f use the algorithm as a simple method of distinguishing valid numbers from mistyped or otherwise incorrect numbers.
>>Contents
• `F0af`_`[Description`#description]`_`f
• `F0af`_`[Example for computing check digit`#example-for-computing-check-digit]`_`f
• `F0af`_`[Example for validating check digit`#example-for-validating-check-digit]`_`f
• `F0af`_`[Strengths and weaknesses`#strengths-and-weaknesses]`_`f
• `F0af`_`[Pseudocode implementation`#pseudocode-implementation]`_`f
• `F0af`_`[Uses`#uses]`_`f
• `F0af`_`[References`#references]`_`f
• `F0af`_`[Notes`#notes]`_`f
• `F0af`_`[External links`#external-links]`_`f
-─
>>Description
The check digit is computed as follows:
1. Drop the check digit from the number (if it's already present). This leaves the payload.
2. Start with the payload digits. Moving from right to left, double every second digit, starting from the last digit. If doubling a digit results in a value > 9, subtract 9 from it (or sum its digits).
3. Sum all the resulting digits (including the ones that were not doubled).
4. The check digit is calculated by ( 10 − − ( s mod 1 0 ) ) mod 1 0 {\\displaystyle (10-(s{\\bmod {1}}0)){\\bmod {1}}0} , where s is the sum from step 3. This is the smallest number (possibly zero) that must be added to s {\\displaystyle s} to make a multiple of 10. Other valid formulas giving the same value are 9 − − ( ( s + 9 ) mod 1 0 ) {\\displaystyle 9-((s+9){\\bmod {1}}0)} , ( 10 − − s ) mod 1 0 {\\displaystyle (10-s){\\bmod {1}}0} , and 10 ⌈ ⌈ s / 10 ⌉ ⌉ − − s {\\displaystyle 10\\lceil s/10\\rceil -s} . Note that the formula ( 10 − − s ) mod 1 0 {\\displaystyle (10-s){\\bmod {1}}0} will not work in all environments due to differences in how negative numbers are handled by the `F33f`_`[modulo`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Modulo]`_`f operation.
>>>Example for computing check digit
Assume an example of an account number 1789372997 (just the "payload", check digit not yet included):
`t
| Digits reversed | 7 | 9 | 9 | 2 | 7 | 3 | 9 | 8 | 7 | 1 |
|---|---|---|---|---|---|---|---|---|---|---|
| Multipliers | 2 | 1 | 2 | 1 | 2 | 1 | 2 | 1 | 2 | 1 |
| | = | = | = | = | = | = | = | = | = | = |
| | 14 | 9 | 18 | 2 | 14 | 3 | 18 | 8 | 14 | 1 |
| Sum digits | 5 (1+4) | 9 | 9 (1+8) | 2 | 5 (1+4) | 3 | 9 (1+8) | 8 | 5 (1+4) | 1 |
`t
The sum of the resulting digits is 56.
The check digit is equal to ( 10 − − ( 56 mod 1 0 ) ) mod 1 0 = 4 {\\displaystyle (10-(56{\\bmod {1}}0)){\\bmod {1}}0=4} .
This makes the full account number read 17893729974.
>>>Example for validating check digit
1. Drop the check digit (last digit) of the number to validate. (e.g. 17893729974 → 1789372997)
2. Calculate the check digit (see above)
3. Compare your result with the original check digit. If both numbers match, the result is valid. (e.g. (givenCheckDigit = calculatedCheckDigit) ⇔ (isValidCheckDigit)).
>>Strengths and weaknesses
The Luhn algorithm will detect all single-digit errors, as well as almost all transpositions of adjacent digits. It will not, however, detect transposition of the two-digit sequence `*09`* to `*90`* (or vice versa). It will detect most of the possible twin errors (it will not detect `*22`* ↔ `*55`*, `*33`* ↔ `*66`* or `*44`* ↔ `*77`*).
Other, more complex check-digit algorithms (such as the `F33f`_`[Verhoeff algorithm`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Verhoeff_algorithm]`_`f and the `F33f`_`[Damm algorithm`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Damm_algorithm]`_`f) can detect more transcription errors. The `F33f`_`[Luhn mod N algorithm`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Luhn_mod_N_algorithm]`_`f is an extension that supports non-numerical strings.
Because the algorithm operates on the digits in a right-to-left manner and zero digits affect the result only if they cause shift in position, zero-padding the beginning of a string of numbers does not affect the calculation. Therefore, systems that pad to a specific number of digits (by converting 1234 to 0001234 for instance) can perform Luhn validation before or after the padding and achieve the same result.
The algorithm appeared in a United States Patent`:cite-ref-us2950048a-1-1[`F5bf`_`[1`#cite-note-us2950048a-1]`_`f] for a simple, hand-held, mechanical device for computing the checksum. The device took the mod 10 sum by mechanical means. The `*substitution digits`*, that is, the results of the double and reduce procedure, were not produced mechanically. Rather, the digits were marked in their permuted order on the body of the machine.
>>Pseudocode implementation
The following function takes a card number, including the check digit, as an array of integers and outputs `!true`! if the check digit is correct, `!false`! otherwise.
`B100`F9d9function isValid(cardNumber[1..length])`f`b
`B100`F9d9 sum := 0`f`b
`B100`F9d9 parity := length mod 2`f`b
`B100`F9d9 for i from 1 to (length - 1) do`f`b
`B100`F9d9 if i mod 2 == parity then`f`b
`B100`F9d9 sum := sum + cardNumber[i]`f`b
`B100`F9d9 elseif cardNumber[i] > 4 then`f`b
`B100`F9d9 sum := sum + 2 * cardNumber[i] - 9`f`b
`B100`F9d9 else`f`b
`B100`F9d9 sum := sum + 2 * cardNumber[i]`f`b
`B100`F9d9 end if`f`b
`B100`F9d9 end for`f`b
`B100`F9d9 return cardNumber[length] == ((10 - (sum mod 10)) mod 10)`f`b
`B100`F9d9end function`f`b
>>Uses
The Luhn algorithm is used in a variety of systems, including:
• `F33f`_`[Credit card numbers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Payment_card_number]`_`f
• `F33f`_`[IMEI numbers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=International_Mobile_Equipment_Identity]`_`f
• `F33f`_`[CUSIP`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=CUSIP]`_`f numbers for North American financial instruments
• `F33f`_`[National Provider Identifier numbers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=National_Provider_Identifier]`_`f in the United States
• `F33f`_`[Canadian`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Canada]`_`f `F33f`_`[social insurance numbers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Social_insurance_number]`_`f
• `F33f`_`[Israeli`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Israeli_identity_card]`_`f ID numbers
• `F33f`_`[South African`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=South_Africa]`_`f ID numbers
• `F33f`_`[South African`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=South_Africa]`_`f Tax reference numbers
• `F33f`_`[Swedish Personal identity numbers`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Personal_identity_number_(Sweden)]`_`f
• `F33f`_`[Swedish`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Sweden]`_`f Corporate Identity Numbers (OrgNr)
• `F33f`_`[Greek`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Greece]`_`f Social Security Numbers (ΑΜΚΑ)
• `F33f`_`[ICCID`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=SIM_card]`_`f of SIM cards
• `F33f`_`[European patent`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=European_Patent_Convention]`_`f application numbers
• Survey codes appearing on McDonald's, Taco Bell, and Tractor Supply Co. receipts
• `F33f`_`[United States Postal Service`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=United_States_Postal_Service]`_`f package tracking numbers use a modified Luhn algorithm`:cite-ref-4[`F5bf`_`[3`#cite-note-4]`_`f]
• Italian VAT numbers (`F33f`_`[Partita Iva`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=VAT_identification_number]`_`f)`:cite-ref-5[`F5bf`_`[4`#cite-note-5]`_`f]
>>References
`:cite-note-us2950048a-1`!1.`! `:citerefluhn1960`aUS patent 2950048A, `F33f`_`[Luhn, Hans Peter`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Hans_Peter_Luhn]`_`f, "Computer for Verifying Numbers", published 23 August 1960, issued 23 August 1960
`:cite-note-3`!2.`! `F0af`_`[↑`#cite-ref-3]`_`f "Annex B: Luhn formula for computing modulus-10 "double-add-double" check digits". `*Identification cards — Identification of issuers — Part 1: Numbering system`* (standard). `F33f`_`[International Organization for Standardization`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=International_Organization_for_Standardization]`_`f & `F33f`_`[International Electrotechnical Commission`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=International_Electrotechnical_Commission]`_`f. January 2017. `F33f`_`[ISO/IEC 7812`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=ISO/IEC_7812]`_`f-1:2017.
`:cite-note-4`!3.`! `F0af`_`[↑`#cite-ref-4]`_`f `*Publication 199: Intelligent Mail Package Barcode (IMpb) Implementation Guide for Confirmation Services and Electronic Payment Systems`* (PDF) (28th ed.). `F33f`_`[United States`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=United_States]`_`f: `F33f`_`[United States Postal Service`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=United_States_Postal_Service]`_`f. 10 October 2023. Archived (PDF) from the original on 17 November 2023. Retrieved 29 November 2023.
`:cite-note-5`!4.`! `F0af`_`[↑`#cite-ref-5]`_`f `:citerefalbanese2022`aAlbanese, Ilenia (10 August 2022). "A cosa serve la Partita Iva? Ecco cosa sapere" [What is a VAT number for? Here's what to know]. `*Partitaiva.it`* (in Italian). Archived from the original on 29 June 2024. Retrieved 29 June 2024.
>>Notes
`:cite-note-2`!a.`! `F0af`_`[↑`#cite-ref-2]`_`f It is described in `F33f`_`[US`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=United_States]`_`f patent 2950048A, granted on 23 August 1960.`:cite-ref-us2950048a-1-0[`F5bf`_`[1`#cite-note-us2950048a-1]`_`f]
>>External links
• Luhn test of credit card numbers on `F33f`_`[Rosetta Code`:/page/wikibook/entry.mu`zim=wikipedia_en_all_nopic_2025-08.zim|entry_path=Rosetta_Code]`_`f: Luhn algorithm/formula implementation in 160 programming languages as of 22 July 2024
`c`F0af`_`[↑ Back to top`#top]`_`f`a